import {
	mapState,
	mapMutations,
	mapActions,
	mapGetters
} from 'vuex';
import {
	topicTypeListEnum
} from '@/utils/enumeration.js';
import {
	debounce
} from '@/utils/base.js';
// 注入全局minix
export default {
	data() {
		return {}
	},
	computed: {
		...mapState({
			minProjectLogin: (state) => state.minProjectStore.minProjectLogin, //小程序是否登录
			showAnswerProgress: (state) => state.minProjectStore.showAnswerProgress, //跳转到答题进度页面
			showSendTempTestPaper: (state) => state.minProjectStore.showSendTempTestPaper, //跳转到临时题答题进度页面
			minStopTemp: (state) => state.minProjectStore.minStopTemp, //小程序临时题结束
			minStopTest: (state) => state.minProjectStore.minStopTest, //小程序随堂检测结束
			minStopHomework: (state) => state.minProjectStore.minStopHomework, //小程序课后作业结束
			showsendHomework: (state) => state.minProjectStore.showsendHomework, //小程序课后作业发题
			showhomeworkUpdateStart: (state) => state.minProjectStore.showhomeworkUpdateStart, //小程序课后作业收取
			minfinishCourse: (state) => state.minProjectStore.minfinishCourse, //小程序下课
			showTempTrueAnswerRank: (state) => state.minProjectStore.showTempTrueAnswerRank, //结束临时题时选择答案跳转统计排名
			minWebsockdk: (state) => state.minProjectStore.minWebsockdk, //小程序意外断开
			mingetAnswerProgress: (state) => state.minProjectStore.mingetAnswerProgress, //继续推送进度
			mingetsendTime: (state) => state.minProjectStore.mingetsendTime, //继续推送进度
			sendTopicData: (state) => state.websockStore.sendTopicData, //小程序已发试题
			deviceType: (state) => state.websockStore.deviceType, //设备类型
			minProjectConncted: (state) => state.minProjectStore.conncted,
			rabbitmqConncted: (state) => state.rabbitMQStore.conncted,
			showResources: (state) => state.minProjectStore.showResources, //显示资源
		}),
	},
	watch: {
		/* 监听小程序上课意外断开 */
		minWebsockdk: {
			handler(newValue, oldValue) {
				console.log('到这里额吗minWebsockdk', this.minProjectLogin);
				console.log('到这里额吗下课吗', this.minfinishCourse);
				/* 同步到小程序 */
				if (newValue > 0 && this.minProjectLogin > 0) {
					/* if(this.minfinishCourse>0){
						this.$message.error('移动端已断开控制PC端,如需继续上课请重新扫码',10);
						sessionStorage.setItem('isHasWxLoginTips','');//是否有小程序控制pc端提示
						return
					} */
					this.$store.state.minProjectStore.minProjectLogin = 0;
					this.$message.error('移动端已断开控制PC端,如需继续操作上课请重新扫码');
					sessionStorage.setItem('isHasWxLoginTips', ''); //是否有小程序控制pc端提示
				}
			},
			immediate: true, //初始化时让handler调用一下
			deep: true, // 开启深度监听
		},
		/* 监听设备信息，同步到小程序 */
		syncBaseData: {
			handler(newValue, oldValue) {
				console.log('syncBaseData', this.minProjectLogin);
				/* 同步到小程序 */
				if (this.minProjectLogin) {
					this.debounceSyncBaseData();
				}
				if (this.minfinishCourse > 0) {
					return
				}
				let isHasWxLoginTips = sessionStorage.getItem('isHasWxLoginTips')
				if (this.minProjectLogin > 0 && !isHasWxLoginTips) {
					this.$message.success('移动端正在控制PC端');
					sessionStorage.setItem('isHasWxLoginTips', 1)
				}
			},
			immediate: true, //初始化时让handler调用一下
			deep: true, // 开启深度监听
		},
		//小程序随堂检测答题进度
		showAnswerProgress: {
			handler(newValue, oldValue) {
				/* 同步到小程序 */
				console.log('showAnswerProgress数据呀呀呀呀' + typeof newValue);
				//console.log('showAnswerProgress数据呀呀呀呀' + (newValue > 0));
				//let tempTestData = this.$ls.get('tempTestData');

				//console.log('temp数据呀呀呀呀' + JSON.stringify(tempTestData));
				if (newValue && newValue.num > 0 && this.minProjectLogin) {
					let beginClassData = this.$ls.get('beginClass');
					this.$ls.set('beginClass', Object.assign({}, {
						...beginClassData
					}, {
						topicType: newValue.topicType,
					}));
					//如果正在答题，先需要结束答题
					let newtestData = this.$ls.get('testData');
					console.log('showAnswerProgress---newtestData' + JSON.stringify(newtestData));
					//console.log('this.isAnswering：'+(typeof this.isAnswering) + this.isAnswering)

					this.beforeSend().then(da => {
						newtestData.state = -1;
						newtestData.sendState = -1;
						newtestData.testQuestId = newValue.testQuestId;
						newtestData.istempTestTime = newValue.istempTestTime;
						newtestData.isSbtive = newValue.isSbtive;
						newtestData.testTime = newValue.testTime;
						this.$ls.set('testData', newtestData)
						this.sendTest(newtestData);
					})

				}
			},
			immediate: true, //初始化时让handler调用一下
			deep: true, // 开启深度监听
		},
		//小程序临时题发题
		showSendTempTestPaper: {
			handler(newValue, oldValue) {
				console.log('数据呀呀呀呀' + JSON.stringify(newValue));
				console.log('this.minProjectLogin' + JSON.stringify(this.minProjectLogin));

				/* 同步到小程序 */
				if (this.minProjectLogin) {
					/* if (newValue && Object.keys(newValue).length > 0 && newValue.sdTestQuestId && newValue.num > 0) { */
					if (newValue && Object.keys(newValue).length > 0 && newValue.num > 0) {
						// this.getDefaultCompnent();
						let tempTestData = this.$ls.get('tempTestData');
						let newtestData = this.$ls.get('testData');

						let beginClassData = this.$ls.get('beginClass');
						this.$ls.set('beginClass', Object.assign({}, {
							...beginClassData
						}, {
							topicType: newValue.topicType,
						}));

						this.beforeSend().then(da => {
							this.sendTemp(newValue);
						})

					}
				}
			},
			immediate: true, //初始化时让handler调用一下
			deep: true, // 开启深度监听
		},
		//小程序临时题结束答题
		minStopTemp: {
			handler(newValue, oldValue) {
				console.log("小程序结束newValue" + JSON.stringify(newValue))
				console.log("小程序结束oldValue" + JSON.stringify(oldValue))
				/* 同步到小程序 */
				if (newValue > 0 && this.minProjectLogin) {
					this.$nextTick(() => {
						this.$refs.componenttempAnswer && this.$refs.componenttempAnswer.stopAnswer(
							'wxStopTemp');
					});
				}
			},
			immediate: true, //初始化时让handler调用一下
			deep: true, // 开启深度监听
		},

		//小程序临时题结束选择正确答案跳转排名统计页面
		showTempTrueAnswerRank: {
			handler(newValue, oldValue) {
				/* 同步到小程序 */
				if (this.minProjectLogin) {
					if (newValue && newValue != oldValue && newValue.num > 0) {
						let newData = {
							...newValue
						};
						newData.topicType = this.formData.topicType;
						this.$nextTick(() => {
							this.wxStopTrueAnswer(newData)
							// this.$refs.componenttempAnswer && this.$refs.componenttempAnswer.wxStopTrueAnswer(
							// 	newData);
						});
					}
				}
			},
			immediate: true, //初始化时让handler调用一下
			deep: true, // 开启深度监听
		},
		//小程序随堂检测结束答题
		minStopTest: {
			handler(newValue, oldValue) {
				/* 同步到小程序 */
				console.log('微信结束随堂检测start' + newValue);
				if (newValue > 0 && this.minProjectLogin) {
					let sdTestQuestId = localStorage.getItem('sdTestQuestId');
					console.log('微信结束随堂检测start' + sdTestQuestId);
					//if (sdTestQuestId) {
					this.$nextTick(() => {
						console.log('微信结束随堂检测start' + sdTestQuestId);
						this.$refs.componentanswerProgress && this.$refs.componentanswerProgress.stopAnswer(
							'wxStopTest');
						//this.$refs.componentquiz.stopTestAnswer('wxStopTest');
					});
					//}
				}
			},
			immediate: true, //初始化时让handler调用一下
			deep: true, // 开启深度监听
		},
		//小程序课后作业结束收取
		minStopHomework: {
			handler(newValue, oldValue) {
				/* 同步到小程序 */
				if (newValue > 0 && this.minProjectLogin) {
					let sdTestQuestId = localStorage.getItem('sdTestQuestId');
					console.log('微信结束随堂检测start' + sdTestQuestId);
					if (sdTestQuestId) {
						this.$nextTick(() => {
							console.log('微信结束随堂检测start' + sdTestQuestId);
							//this.$refs.componentquiz.stopTestAnswer('wxStopTest');
							this.$refs.componentanswerProgress && this.$refs.componentanswerProgress.stopAnswer(
								'wxStopTest');
						});
					}
				}
			},
			immediate: true, //初始化时让handler调用一下
			deep: true, // 开启深度监听
		},
		//小程序课后作业发题
		showsendHomework: {
			handler(newValue, oldValue) {
				/* 同步到小程序 */
				if (this.minProjectLogin) {
					if (newValue && newValue != oldValue && newValue.num > 0) {
						this.oldcurrentKey = this.currentKey;
						this.currentKey = 'testPaper';
						this.checkMenu({
							type: this.currentKey
						})
						this.getTestquestDetails(newValue.testQuestId, newValue.isSbtive);
						this.changeTab(99, {
							testQuestId: newValue.testQuestId,
							isSbtive: newValue.isSbtive,
							currentKey: 'answerProgress'
						});
						this.$nextTick(() => {
							this.getDefaultCompnent('wx');
						});
					}
				}
			},
			immediate: true, //初始化时让handle
			deep: true, // 开启深度监听
		},
		//小程序课后作业收取
		showhomeworkUpdateStart: {
			handler(newValue, oldValue) {
				/* 同步到小程序 */
				if (this.minProjectLogin) {
					if (newValue && newValue != oldValue && newValue.num > 0) {
						let beginClassData = this.$ls.get('beginClass');
						if (newValue.csId == beginClassData.csId) {

							this.beforeSend().then(da => {
								this.homeworkUpdateStart(newValue)
							});
						}
					}
				}
			},
			immediate: true, //初始化时让handle
			deep: true, // 开启深度监听
		},
		//小程序下课
		minfinishCourse: {
			handler(newValue, oldValue) {
				/* 同步到小程序 */
				if (newValue > 0 && this.minProjectLogin) {
					this.$store.state.minProjectStore.minProjectLogin = 0;
					this.endClass();
				}
			},
			immediate: true, //初始化时让handle
			deep: true, // 开启深度监听
		},
		//向微信推送总的作答进度
		mingetAnswerProgress: {
			handler(newValue, oldValue) {
				/* 同步到小程序 */
				if (newValue && newValue.num > 0 && this.minProjectLogin) {
					let newtestData = this.$ls.get('testData');
					this.$refs.componenttempAnswer && this.$refs.componenttempAnswer.totalpercentUpdate();
					this.$refs.componentanswerProgress && this.$refs.componentanswerProgress.totalpercentUpdate();
					/* if(newValue.testQuestId&&(newValue.testQuestId==newtestData.testQuestId)){
						this.$refs.componentquiz&&this.$refs.componentquiz.totalpercentUpdate();
					} */
					//this.$refs.componenttempAnswer&&this.$refs.componenttempAnswer.totalpercentUpdate();
				}
			},
			immediate: true, //初始化时让handle
			deep: true, // 开启深度监听
		},
		//向微信推送试题时长和倒计时（查看进度）
		mingetsendTime: {
			handler(newValue, oldValue) {
				/* 同步到小程序 */
				if (newValue && newValue.num > 0 && this.minProjectLogin) {
					console.log("ddddddddddddddddddddddddddddd")
					let tempTestData = this.$ls.get('tempTestData');
					let newtestData = this.$ls.get('testData');

					let beginClassData = this.$ls.get('beginClass');
					this.$ls.set('beginClass', Object.assign({}, {
						...beginClassData
					}, {
						topicType: newValue.topicType,
					}));

					this.beforeSend(newValue.testQuestId).then(da => {
						newtestData.state = newValue.state;
						newtestData.sendState = newValue.sendState;
						newtestData.testQuestId = newValue.testQuestId;
						//newtestData.topicType=newValue.topicType;//不要
						newtestData.isSbtive = newValue.isSbtive;
						newtestData.testTime = newValue.testTime;
						this.$ls.set('testData', newtestData);
						this.sendTest(newtestData, 1);
					})
					return
				}
			},
			immediate: true, //初始化时让handle
			deep: true, // 开启深度监听
		},
		//火狐浏览器会在 sdk断开重联的时候，断开小程序连接和rabbitmq连接
		//如果小程序还连接，但是pc端连接的websocket断开
		minProjectConncted: {
			handler(newValue, oldValue) {
				console.log("1走到这里了吗重连" + this.minProjectLogin)
				console.log("newValue走到这里了吗重连" + newValue)
				if (!newValue && this.minProjectLogin) {
					console.log("2走到这里了吗重连" + this.minProjectLogin)
					//去重连
					/* 监听小程序websocket */
					let beginClassData = this.$ls.get('beginClass'); //上课数据
					this.MINPROJECT_RECONNECT(process.env.VUE_APP_MINPROJECT_API + beginClassData.csId);
				}
			},
			immediate: true, //初始化时让handle
			deep: true, // 开启深度监听
		},
		//如果小程序还连接,但是rabbitmq 断开，需要重连
		rabbitmqConncted: {
			handler(newValue, oldValue) {
				if (!newValue && this.minProjectLogin && this.deviceType == 'wired') {
					//去重连
					this.RABBITMQ_INIT();
				}
			},
			immediate: true, //初始化时让handle
			deep: true, // 开启深度监听
		},
		//显示资源
		showResources: {
			handler(newValue, oldValue) {
				console.log('showResources', this.showResources)
				/* 同步到小程序 */
				if (this.minProjectLogin) {
					if (newValue != oldValue) {
						if ((newValue > 0 && this.currentKey != 'resources') || (newValue == 0 && this.currentKey ==
								'resources')) {
							this.checkMenu({
								type: 'resources'
							})
						}

					}
				}
			},
			immediate: true, //初始化时让handle
			deep: true, // 开启深度监听
		}
	},
	methods: {
		...mapMutations([
			'SET_sdTestQuestId',
			'WEBSOCKET_SEND',
			'SET_sendTopicData',
			'SET_isAnswering',
			'MINPROJECT_RECONNECT',
			'RABBITMQ_INIT'
		]),
		//发送临时题
		sendTemp(newValue) {
			console.log("发题临时题了，啊" + newValue.topicType)
			//if(!(this.isAnswering-0)){
			// this.formData.topicType = newValue.topicType;
			this.oldcurrentKey = this.currentKey;
			this.currentKey = 'tempAnswer';
			this.checkMenu({
				type: 'tempAnswer'
			});
			if (this.$refs.componenttempAnswer) {
				this.$refs.componenttempAnswer.isCountDown = false;
			}
			if (this.$refs.componentanswerProgress) {
				this.$refs.componentanswerProgress.isCountDown = false;
			}
			this.changeTab(newValue.topicType);
			setTimeout(() => {
				this.$nextTick(() => {
					this.wxSendTempSuccess(newValue)
					//this.$refs.componenttempAnswer && this.$refs.componenttempAnswer.wxSendSuccess(newValue);
				});
			}, 200)
		},
		//发题前处理逻辑--关闭上一题，关闭需要关闭的指令
		beforeSend(testQuestId) {
			//关闭上一题
			let stopLen = 0;
			if (this.sendTopicData.list && this.sendTopicData.list.length > 0) {
				console.log("this.sendTopicData.list?>>>>", this.sendTopicData.list)
				//如果只是查看进度，不要结束这道题
				let list = [...this.sendTopicData.list].filter(item => !testQuestId || testQuestId && item
					.testQuestId != testQuestId);
				this.SET_sendTopicData({
					csId: this.sendTopicData.csId,
					list: [],
				});
				if (list && list.length > 0) {
					list.forEach(item => {
						this.$postAction(this.$sysManageapi.answerUrl.stopAnswer, item.params)
					})
					stopLen = 1;

					//有线
					if (this.deviceType == 'wired') {
						//发送结束指令
						this.WEBSOCKET_SEND({
							fun: 'answer_stop',
						});
					}

				}
			}
			//关闭指令
			let len = this.stopDeviceEventList.length;
			if (len > 0) {
				for (let i = 0; i < len; i++) {
					setTimeout(() => {
						this.WEBSOCKET_SEND({
							fun: this.stopDeviceEventList[i],
						});
						if (i == len - 1) {
							this.SET_STOPDEVICEEVENTLIST([]);
						}
					}, 50 * i);
				}
			}
			return new Promise(resolve => setTimeout(resolve, 50 * (len + stopLen)));
		},
		//结束临时题时选择答案跳转统计排名
		wxStopTrueAnswer(data) {
			console.log("结束临时题时选择答案跳转统计排名" + JSON.stringify(data))
			let totalanswer = Object.keys(data).length > 0 ? (this.formData.topicType == 1 ? data.totalanswer.split(
				'') : data.totalanswer) : '';
			let operateType = '';
			let answerType = '';
			//停止答题，如果发题前没有输入正确答案 跳转到选择正确答案页面，有输入则跳转统计页面，并查看作答结果
			if (
				totalanswer &&
				totalanswer.length > 0 &&
				Object.keys(totalanswer).length != 0 &&
				totalanswer != undefined
			) {
				operateType = 'rank';
				answerType = 'count';
				//设置是否正在答题
				this.SET_isAnswering(0);
				//this.getTempAnswerRecLisList();
			} else {
				operateType = 'answer';
				answerType = 'over';
			}
			let tempTestData = this.$ls.get('tempTestData');
			console.log('临时题上海市实话实说' + JSON.stringify(tempTestData));
			let newData = Object.assign({}, {
				...tempTestData
			}, {
				totalanswer: totalanswer,
				operateType: operateType,
				answerType: answerType,
			})
			//临时题数据缓存
			this.$ls.set('tempTestData', newData);
			this.$refs.componenttempAnswer.operateType = operateType;
			this.$refs.componenttempAnswer.answerType = answerType;
			this.$refs.componenttempAnswer.totalanswer = totalanswer;
			this.$refs.componenttempAnswer.setRankData();
			//移动端控制pc端，存储发题数据
			if (this.minProjectLogin) {
				console.log("很好看哈酒哈科技" + JSON.stringify(this.sendTopicData))
				if (Object.keys(this.sendTopicData).length > 0 && this.sendTopicData && this.sendTopicData.list.length >
					0) {
					let newList = this.sendTopicData.list.filter(item => item.topicType == 99)
					//this.sendTopicData.list = [...newList];
					this.SET_sendTopicData({
						csId: this.formData.csId,
						list: newList,
					})
				}

			}
		},
		//发送临时题成功
		async wxSendTempSuccess(data) {
			/* console.log("临时题发送成功", JSON.stringify(data)) */
			let {
				csId,
				bigNum,
				subNum,
				anScopeEnd,
				anScopeStart,
				topicType,
				testQuestId,
				sdTestQuestId,
				totalanswer,
				testTime
			} = data;

			//新增临时题试题
			let tempTopicDataItem = await this.addTestPaper(data);
			console.log('新增临时返回了试题id吗？' + JSON.stringify(tempTopicDataItem));
			if (tempTopicDataItem && tempTopicDataItem.testQuestId) {
				this.sendTempTestPaper({}, tempTopicDataItem, data);
			}
		},

		//临时题新增试题
		async addTestPaper(data) {
			let answer = data.totalanswer;
			let questions = [];
			let bigNum = 1;
			let subNum = 1;
			//临时题题号信息
			let tempTopicData = this.$ls.get('tempTopicData');
			//这里取得是缓存里面的
			if (tempTopicData && tempTopicData.topicList && tempTopicData.topicList.length > 0) {
				//比对是否是这个课题的
				if (this.formData.csId == tempTopicData.csId) {
					let lastTopic = tempTopicData.topicList[tempTopicData.topicList.length - 1];
					let lastSubNum = lastTopic.subNum == 9999 ? 1 : lastTopic.subNum + 1;
					var lastBigNum = lastSubNum == 9999 ? lastTopic.bigNum + 1 : lastTopic.bigNum;
					bigNum = lastBigNum;
					subNum = lastSubNum;
				}
			}
			var param = {
				bigNum,
				subNum,
				topicType: this.formData.topicType,
				scoreValue: data.scoreValue,
				partScoreValue: data.partScoreValue,
				answer: answer,
				anScopeStart: data.anScopeStart,
				anScopeEnd: data.anScopeEnd,
			};
			questions.push(param);

			/* 处理如果没有倒计时，后端要求不传该参数 */
			/* if (!this.formInfo.testTime && this.formInfo.testTime != 0) {
				delete this.formInfo.testTime;
			} */
			var parameter = Object.assign({}, {
				schTopicDtos: questions,
				status: 1,
				deptId: this.formData.deptId, //校区
				title: '临时题',
				csId: this.formData.csId, //课题id
				isWire: this.deviceType == 'wired' ? 1 : 0,
			});
			return new Promise((resolve, reject) => {
				this.$postAction(this.$sysManageapi.testquestUrl.insertTmp, parameter)
					.then((da) => {
						if (da.code == 1) {
							resolve({
								bigNum,
								subNum,
								testQuestId: da.data.testQuestId,
								topicId: da.data.topicId,
							});
						} else {
							resolve();
						}
					})
					.catch((err) => {
						//返回错误
						reject(err);
					});
			});
		},
		//发送临时题
		sendTempTestPaper(importTopic, tempTopicDataItem, data) {
			//有线
			if (this.deviceType == 'wired') {
				//发送题目
				let questions = [];
				let totalanScopeStart = '';
				let totalanScopeEnd = '';
				let totalbigNum = '';
				let totalsubNum = '';
				let questionType = '';
				let sendMsg = '';
				//如果是导入的题目吗
				if (importTopic && JSON.stringify(importTopic) != '{}') {
					questionType = topicTypeListEnum.find((topicitem) => topicitem.value == importTopic.topicType).type;
					totalanScopeStart = importTopic.anScopeStart;
					totalanScopeEnd = importTopic.anScopeEnd;
					totalbigNum = importTopic.bigNum;
					totalsubNum = importTopic.subNum;
				} else {
					let topicitem = topicTypeListEnum.find((topicitem) => topicitem.value == this.formData.topicType);
					questionType = this.machineData.wareVersion == 'RP554' ? topicitem.videotype : topicitem.type;
					totalanScopeStart = data.anScopeStart;
					totalanScopeEnd = data.anScopeEnd;
					totalbigNum = tempTopicDataItem.bigNum;
					totalsubNum = tempTopicDataItem.subNum;
					sendMsg = topicitem.text + ':' + totalbigNum + '-' + totalsubNum;
				}

				//特别处理判断题，数字题答案范围
				let range = '';
				if (questionType == 'j') {
					range = '';
				} else if (questionType == 'd') {
					range = '0-9999';
				} else {
					range = totalanScopeStart + '-' + totalanScopeEnd;
				}

				if (this.machineData.wareVersion == 'RP554') {
					let msg = {
						fun: 'answer_start',
						uID: 0,
						type: questionType,
						ans_id: importTopic.topicId || tempTopicDataItem.topicId,
					};
					this.WEBSOCKET_SEND(msg);
					//发送回显
					this.WEBSOCKET_SEND({
						fun: 'dtq_echo_set',
						uID: 0,
						msg: sendMsg,
					});
				} else {
					questions = [{
						type: questionType,
						id: totalbigNum + '-' + totalsubNum,
						range: range,
					}, ];

					let msg = {
						fun: 'answer_start',
						questions: questions,
						ans_id: totalbigNum + '-' + totalsubNum,
					};
					this.$ls.set('answer_start', {
						...msg,
					}); //保存发题信息
					this.WEBSOCKET_SEND(msg);
				}

				// questions = [{
				// 	type: questionType,
				// 	id: totalbigNum + '-' + totalsubNum,
				// 	range: range,
				// }, ];

				// let msg = {
				// 	fun: 'answer_start',
				// 	questions: questions,
				// };
				// this.WEBSOCKET_SEND(msg);
			}
			let paramter = {};
			if (importTopic && JSON.stringify(importTopic) != '{}') {
				paramter.schTopicDtos = [{
					bigNum: importTopic.bigNum,
					subNum: importTopic.subNum,
					anScopeStart: importTopic.anScopeStart,
					anScopeEnd: importTopic.anScopeEnd,
					topicType: importTopic.topicType,
					topicId: importTopic.topicId,
				}, ];
				paramter.testTime = importTopic.testTime;
				paramter.testQuestId = this.formData.testQuestId;

				console.log('发送导入的题' + JSON.stringify(importTopic));
			} else {
				console.log('testQuestId>>>>>>>' + tempTopicDataItem.testQuestId);
				paramter.schTopicDtos = [{
					bigNum: tempTopicDataItem.bigNum,
					subNum: tempTopicDataItem.subNum,
					anScopeStart: data.anScopeStart,
					anScopeEnd: data.anScopeEnd,
					topicType: this.formData.topicType,
					topicId: tempTopicDataItem.topicId,
				}, ];
				paramter.testTime = data.testTime;
				paramter.testQuestId = tempTopicDataItem.testQuestId;
			}
			paramter.classId = this.formData.classId;
			paramter.csId = this.formData.csId;
			paramter.isWire = this.deviceType == 'wired' ? 1 : 0;
			//如果是无线加入是否自动作答
			if (this.deviceType != 'wired') {
				paramter.isAuto = this.isAuto ? 1 : 0;
			}
			console.log('临时题发题参数' + JSON.stringify(paramter));
			return this.$postAction(this.$sysManageapi.answerUrl.sendTempTestPaper, paramter).then((res) => {
				if (res && res.code == 1) {
					let operateType = 'answer';
					let answerType = 'answer';
					let newtotalanswer = this.formData.topicType == 1 ? (data.totalanswer ? data.totalanswer
						.split('') : []) : data.totalanswer;
					this.$refs.componenttempAnswer.isCountDown = !!data.testTime;
					this.$refs.componenttempAnswer.timeDiffInSeconds = data.testTime ? data.testTime * 60 : 0;
					this.$refs.componenttempAnswer.totalanswer = newtotalanswer;
					console.log('前端发题当前时间' + JSON.stringify(moment().format('YYYY-MM-DD HH:mm:ss')));
					console.log(
						'后端返回发题时间' + JSON.stringify(moment(res.data.sendTime).format('YYYY-MM-DD HH:mm:ss'))
					);
					//如果是发导入的试题，将发题状态改为已发送
					if (importTopic && JSON.stringify(importTopic) != '{}') {
						this.$refs.tempImportAnswer.importTopic.isSend = true;
						importTopic.tempsdTestQuestId = res.data.sdTestQuestId;
						this.importTopicData = {
							...importTopic,
						};
						//存储导入试题的当前发题情况
						this.$ls.set('importTopic', importTopic), console.log('发题id' + res.data.sdTestQuestId);
					} else {
						this.$refs.componenttempAnswer.operateType = operateType;
						this.$refs.componenttempAnswer.answerType = answerType;
						//获取临时题发题题目数据
						let topicList = [];
						let tempTopicData = this.$ls.get('tempTopicData');
						if (tempTopicData && JSON.stringify(tempTopicData) != '{}') {
							topicList = [...tempTopicData.topicList];
						}
						topicList.push({
							bigNum: tempTopicDataItem.bigNum,
							subNum: tempTopicDataItem.subNum,
							topicId: tempTopicDataItem.topicId,
						});
						//存储临时题发题题目数据
						this.$ls.set('tempTopicData', {
							csId: this.formData.csId,
							topicList,
						});
					}
					//存储发题id
					localStorage.setItem('tempsdTestQuestId', res.data.sdTestQuestId);
					console.log('存储发题Id' + res.data.sdTestQuestId);
					//设置是否正在答题
					this.SET_isAnswering(1);
					//储存临时题发题时间戳
					let tempsendTime = new Date(res.data.sendTime).getTime();
					localStorage.setItem('tempsendTime', tempsendTime);
					//保存一次本机的发题时间。防止计算答题用时时有误差
					localStorage.setItem('sysTempsendTime', new Date().getTime());

					let tempTestData = this.$ls.get('tempTestData');
					console.log('临时题上海市实话实说' + JSON.stringify(tempTestData));
					//临时题数据缓存
					let newData = Object.assign({}, {
						...tempTestData
					}, {
						"bigNum": tempTopicDataItem.bigNum,
						"subNum": tempTopicDataItem.subNum,
						"anScopeEnd": data.anScopeEnd,
						"anScopeStart": data.anScopeStart,
						"testQuestId": tempTopicDataItem.testQuestId,
						totalanswer: newtotalanswer,
						testTime: data.testTime,
						operateType: operateType,
						answerType: answerType,
					})

					console.log("newData" + JSON.stringify(newData))
					this.$ls.set('tempTestData', newData);

					//清空上一题作答数据
					this.$refs.componenttempAnswer.list.forEach((item) => {
						item.state = 0;
						item.seconds = null;
						item.answerResult = '';
					});

					//移动端控制pc端，存储发题数据
					if (this.minProjectLogin) {
						let list = [];
						if (Object.keys(this.sendTopicData).length > 0 && this.sendTopicData && this
							.sendTopicData.list.length >
							0) {
							list = [...this.sendTopicData.list];
							list.push({
								testQuestId: tempTopicDataItem.testQuestId,
								sdTestQuestId: res.data.sdTestQuestId,
								topicType: this.formData.topicType,
								params: {
									classId: this.formData.classId,
									sdTestQuestId: res.data.sdTestQuestId,
									isWire: this.deviceType == 'wired' ? 1 : 0,
								}
							})
						} else {
							list = [{
								testQuestId: tempTopicDataItem.testQuestId,
								sdTestQuestId: res.data.sdTestQuestId,
								topicType: this.formData.topicType,
								params: {
									classId: this.formData.classId,
									sdTestQuestId: res.data.sdTestQuestId,
									isWire: this.deviceType == 'wired' ? 1 : 0,
								}
							}];
						}
						this.SET_sendTopicData({
							csId: this.formData.csId,
							list: list,
						})
					}
					this.sendWx({
						fun: 'sendTestSuccess',
						csId: this.formData.csId,
						data: JSON.stringify({
							testQuestId: tempTopicDataItem.testQuestId,
							sdTestQuestId: res.data.sdTestQuestId,
							topicId: tempTopicDataItem.topicId
						}),
					});
					this.$nextTick(() => {
						this.getDefaultCompnent('wx');
					})
				}
			});
		},
		//发送套题
		sendTest(testData, type = 0) {
			this.currentKey = 'answerProgress';
			this.oldcurrentKey = 'testPaper';
			this.changeTab(99, {
				testQuestId: testData.testQuestId,
				isSbtive: testData.isSbtive,
				currentKey: 'answerProgress'
			});
			this.checkMenu({
				type: 'answerProgress'
			})
			this.$ls.set('testComponet', this.currentKey);
			if (!type) {
				let len = this.stopDeviceEventList.length;
				if (len > 0) {
					for (let i = 0; i < len; i++) {
						setTimeout(() => {
							this.WEBSOCKET_SEND({
								fun: this.stopDeviceEventList[i],
							});
							if (i == len - 1) {
								this.SET_STOPDEVICEEVENTLIST([]);
							}
						}, 50 * i);
					}
				}
			}
			if (type) {
				//查看进度type:1
				this.$refs.componentanswerProgress && this.$refs.componentanswerProgress.initData();
				//this.$refs.componentquiz.reloadCompAnswerProgress();
			} else {
				if (testData.testType == 1) {
					this.getTestquestDetails(testData.testQuestId, testData.isSbtive);
				} else {
					if (this.$refs.componentanswerProgress) {
						this.$refs.componentanswerProgress.list.forEach(item => {
							item.hasSchTopicVos = [];
							item.percent = 0;
						});

						this.$refs.componentanswerProgress.totalpercent = 0;
						this.$refs.componentanswerProgress && this.$refs.componentanswerProgress.initData();
					}
				}
			}
			this.$nextTick(() => {
				this.getDefaultCompnent('wx');
			});
		},
		//收取课后作业
		homeworkUpdateStart(newValue) {
			this.oldcurrentKey = this.currentKey;
			this.currentKey = 'answerProgress';
			this.checkMenu({
				type: 'answerProgress'
			});
			this.$ls.set('testComponet', this.currentKey);
			if (this.$refs.componentanswerProgress) {
				//防止正在该页面，正在作答随堂检测，需要清空数据
				this.$refs.componentanswerProgress.list.forEach(item => {
					item.hasSchTopicVos = [];
					item.percent = 0;
				});
				this.$refs.componentanswerProgress.totalpercent = 0;
			}
			this.changeTab(99, 'wx', {
				testQuestId: newValue.testQuestId,
				isSbtive: newValue.isSbtive,
				currentKey: 'answerProgress'
			});
			this.$nextTick(() => {
				this.getDefaultCompnent('wx');
			});
		},
		//查询试题详情发送课后作业
		getTestquestDetails(testQuestId, isSbtive) {
			if (testQuestId) {
				return new Promise((resolve, reject) => {
					this.$postAction(this.$sysManageapi.testquestUrl.detail, {
							testQuestId
						})
						.then((da) => {
							console.log("试题想抢" + JSON.stringify(da))
							if (da.code == 1) {
								let schTopicVosData = {
									...da.data
								};
								let newTopic = schTopicVosData.schTopicVos.filter((item) => isSbtive == 0 ?
									(item.topicType != 98) : (item.topicType == 98)); //客观题
								schTopicVosData.schTopicVos = [...newTopic];
								this.$nextTick(() => {
									this.$refs.componenttestPaper.needSendTestInfo = {
										...schTopicVosData
									};
									let schTopicVos = [...newTopic] || [];
									this.$refs.componenttestPaper.needSendTestInfo.testTitle =
										schTopicVosData.title;
									this.$refs.componenttestPaper.needSendTestInfo.isSbtive =
										isSbtive;
									console.log("聚精会神计算机是是是" + JSON.stringify(schTopicVos))
									if (schTopicVos && schTopicVos.length > 0) {
										let len = this.stopDeviceEventList.length;
										if (len > 0) {
											for (let i = 0; i < len; i++) {
												setTimeout(() => {
													this.WEBSOCKET_SEND({
														fun: this
															.stopDeviceEventList[i],
													});
													if (i == len - 1) {
														this.SET_STOPDEVICEEVENTLIST([]);
													}
												}, 50 * i);
											}
										}
										setTimeout(() => {
											this.$refs.componenttestPaper.sendHomeWork()
										}, 50 * len);
									}
								})
								resolve(da.data);
							} else {
								resolve();
							}
						})
						.catch((err) => {
							//返回错误
							reject(err);
						});
				});
			}
		},
		/* 防抖同步学生和设备信息 */
		debounceSyncBaseData: debounce(function() {
			let newstuList = this.studentList.map((item) => {
				return {
					studName: item.studName,
					machineId: item.machineId,
					status: item.status
				}
			})
			this.sendWxMsg({
				fun: 'machineData',
				data: JSON.stringify({
					machineData: this.machineData,
					stopDeviceEventList: this.stopDeviceEventList.length,
					studentList: newstuList,
					deviceType: this.deviceType
				}),
			});
		}, 500),

	}
}